<?php

namespace app\core;


class Logic {


    public function __construct() {
    }

    public static function logicResponse($errno, $error, $result = array()) {
        $response = [
            'errorcode' => $errno,
            'message'   => $error,
            'data'      => $result
        ];

        return $response;
    }

    public static function logicResponseSuccess($result = array()) {
        $response = [
            'errorcode' => 0,
            'message'   => 'success',
            'data'      => $result
        ];

        return $response;
    }

    /**
     * 获取通用的签名算法
     * @param $arrInput
     * @param $arrAuth
     * @return string
     */
    public function getAuthSignStrCommon($arrInput, $arrAuth) {

        $appkey = $arrAuth['appkey'];
        $appid  = $arrAuth['appid'];
        if(!isset($arrInput['appid'])) {
            $arrInput['appid'] = $appid;
        }
        $strSign = '';
        if(isset($arrInput['sign'])) {
            unset($arrInput['sign']);
        }
        $arrInput = array_change_key_case($arrInput, CASE_LOWER);

        // 按key值升序排序
        if(ksort($arrInput)) {

            foreach($arrInput as $key => $value) {
                $strVal = trim($value);
                if($strVal === null || $strVal === '') {
                    continue;
                }
                $strSign .= $key . $strVal;
            }
            $strSign = strtoupper(md5($strSign . $appkey));
        }

        return $strSign;
    }

}
